package com.turing.usdtpay.mvp.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.turing.usdtpay.entity.OrderInfoData;
import com.turing.usdtpay.http.ApiService;
import com.turing.usdtpay.http.ApiUrl;
import com.turing.usdtpay.http.callback.ApiCallback;
import com.turing.usdtpay.http.entity.ResponseDate;
import com.turing.usdtpay.mvp.contract.OrderInfoContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;

/**
 * @作者：JAY
 * @创建时间：2019/11/7 15:51
 * @描述：
 **/
public class OrderInfoPresenter implements OrderInfoContract.Present {
    private OrderInfoContract.View mView;

    public OrderInfoPresenter(OrderInfoContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void getOrderInfo(Activity activity, String order) {
        HashMap<String ,String> map =new HashMap<>();
        map.put("order",order);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_ORDER_INFO, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    Gson gson = new Gson();
                    OrderInfoData data = gson.fromJson(date.getDate(), OrderInfoData.class);
                    mView.onSuccess(data);
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void uploadPhoto(Activity activity, String order, int type, File file) {
        HashMap<String,String> map = new HashMap<>();
        map.put("id",order);
        map.put("type",type+"");
        ApiService.getInstance().uploadFile(activity, ApiUrl.API_UPLOAD_IMG, map, file, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    try {
                        JSONObject object = new JSONObject(date.getDate());
                        String url = object.optString("url");
                        mView.uploadSuccess(url);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }

            @Override
            public void onUIProgress(int progress) {
                super.onUIProgress(progress);
                mView.onProgress(progress);
            }
        });
    }

    @Override
    public void confirmGet(Activity activity, String order) {
        HashMap<String ,String> map =new HashMap<>();
        map.put("order",order);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_GET_CONFIRM, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.getSuccess();
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void confirmPay(Activity activity, String order) {
        HashMap<String ,String> map =new HashMap<>();
        map.put("order",order);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_PAY_CONFIRM, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.paySuccess();
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void cancelOrder(Activity activity, String order) {
        HashMap<String ,String> map =new HashMap<>();
        map.put("order",order);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_ORDER_CANCEL, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.cancelSuccess();
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }


}
